<?php

namespace Acme\TestBundle\Controller;

use Acme\TestBundle\Entity\Student;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class StudentController
 * @package Acme\TestBundle\Controller
 * @Route("/student")
 */
class StudentController extends Controller
{
    /**
     * @Route("/create")
     */
    public function createAction(){
        $student = new Student();
        $student->setName('六');
        $student->setBirth('2017-8-9');
        $student->setSex('1');

        $em = $this->getDoctrine()->getManager();

        $em->persist($student);

        $em->flush();

        var_dump($student->getId());die();
    }

    /**
     * @Route("/get")
     */
    public function getAction(Request $request)
    {
        $repository = $this->getDoctrine()->getRepository('AcmeTestBundle:Student');
        // 学生总成绩
        $sum = $repository->studentScoreSum();
        //平均成绩
        $avg = $repository->studentScoreAvg();
        //01比02高
        $info = $repository->studentScore1();
        //平均成绩大于等于60
        $info = $repository->studentScore2();
        if($request->getMethod() == 'POST'){
            $studentInfo = $repository->studentInfo();
            $return = array(
                'code' => 0,
                'data' => $studentInfo
            );
            return new JsonResponse($return);
        }

        return $this->render('AcmeTestBundle:Default:index.html.twig',compact('info'));

    }
}
